local duohui = fk.CreateSkill{
  name = "steam__duohui",
}

duohui:addEffect(fk.DamageCaused, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self.name) and target==player and data.to
  end,
  on_use = function(self, event, target, player, data)
    local room=player.room
    
    -- 计算需要弃置的牌数（一半手牌，自选取整方向）
    local handcard_num = data.to:getHandcardNum()
    local discard_num = handcard_num
    local up_num=(handcard_num+1)//2
    local down_num=handcard_num//2
    local last_choice=data.to:getMark("@steam__duohui_rounding") or 0
    local choice=room:askToChoice(data.to,{
    choices = {"steam__duohui_rounding_up","steam__duohui_rounding_down"},
    skill_name = self.name,
    prompt = "#steam__duohui-choiceupordown"
     
    })
    if choice=="steam__duohui_rounding_up" then
      discard_num=(handcard_num+1)//2
      room:setPlayerMark(data.to, "@steam__duohui_rounding", "￪")
    else
      discard_num=handcard_num//2
      room:setPlayerMark(data.to, "@steam__duohui_rounding", "￬")
    end
    if discard_num > 0 then
      -- 让受伤角色选择弃牌
      room:askToDiscard(data.to, {
        min_num = discard_num,
        max_num = discard_num,
        include_equip = false,
        skill_name = self.name,
        cancelable = false,
      })
      
       local current_rounding=data.to:getMark("@steam__duohui_rounding")
        
        -- 检查取整方向是否与上次不同
        
    
        
        if last_rounding ~= nil and last_rounding ~= 0 and last_rounding ~= current_rounding then
          data.to:drawCards(3, self.name)
        end
    end
    if discard_num<up_num then
      data:changeDamage(1)
    end
  end,
})




duohui:addEffect(fk.DamageInflicted, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self.name) and target==player and data.to
  end,
  on_use = function(self, event, target, player, data)
    local room=player.room
    
    -- 计算需要弃置的牌数（一半手牌，自选取整方向）
    local handcard_num = data.to:getHandcardNum()
    local discard_num = handcard_num
    local up_num=(handcard_num+1)//2
    local down_num=handcard_num//2
    
    local last_rounding=data.to:getMark("@steam__duohui_rounding") or 0

    local choice=room:askToChoice(data.to,{
    choices = {"steam__duohui_rounding_up","steam__duohui_rounding_down"},
    skill_name = self.name,
    prompt = "#steam__duohui-choiceupordown"
     
    })

    if choice=="steam__duohui_rounding_up" then
      discard_num=(handcard_num+1)//2
      room:setPlayerMark(data.to, "@steam__duohui_rounding", "￪")
    else
      discard_num=handcard_num//2
      room:setPlayerMark(data.to, "@steam__duohui_rounding", "￬")
    end

    if discard_num > 0 then
      -- 让受伤角色选择弃牌
      room:askToDiscard(data.to, {
        min_num = discard_num,
        max_num = discard_num,
        include_equip = false,
        skill_name = self.name,
        cancelable = false,
      })
    end
    local current_rounding=data.to:getMark("@steam__duohui_rounding")
        
    -- 检查取整方向是否与上次不同
    if last_rounding ~= nil and last_rounding ~= 0 and last_rounding ~= current_rounding then
      data.to:drawCards(3, self.name)
    end
    if discard_num<up_num then
      data:changeDamage(1)
    end
  end,
})



Fk:loadTranslationTable{
  ["steam__duohui"] = "堕慧",
  [":steam__duohui"] = "你造成或受到伤害时，受伤角色需弃置一半的手牌（自选取整方向）；若与上次被选择的取整方向不同，则其摸三张牌；若其可弃置更多张牌而未如此做，则此伤害+1。",
  ["#steam__duohui-discard"] = "堕慧：请弃置一半的手牌（自选取整方向）",
  ["@steam__duohui_rounding"] = "堕慧方向",
  ["#steam__duohui-choiceupordown"] = "堕慧：请选择取整方向",
  ["steam__duohui_rounding_up"] = "向上取整",
  ["steam__duohui_rounding_down"] = "向下取整",
}

return duohui 